package cn.com.integration.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.integration.entity.IntegrationMessageContentEntity;
import cn.com.integration.entity.IntegrationMessageEntity;
import cn.com.integration.service.IntegrationMessageContentService;
import cn.com.integration.service.IntegrationMessageService;
import cn.com.util.TsBqUtils;



/**
 * 
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2025-04-15 13:41:27
 */
@Controller
@RequestMapping("/integration")
@Slf4j
public class IntegrationMessageController {
    @Autowired
    private IntegrationMessageService integrationMessageService;
    @Autowired
    private IntegrationMessageContentService integrationMessageContentService;
    
    @PostMapping("/handleMessage")
    @ResponseBody
    public DataResult handleMessage(@RequestBody IntegrationMessageEntity entity){
    	Date date = new Date();
    	String requestMessage = entity.getRequestMessage();
    	if(TsBqUtils.isNull(requestMessage)){
    		return DataResult.fail("requestMessage不能为空！");
    	}
    	
    	String responseMessage = entity.getResponseMessage();
    	if(TsBqUtils.isNull(responseMessage)){
    		return DataResult.fail("responseMessage不能为空！");
    	}
    	String uuid = TsBqUtils.getUUID();
    	entity.setId(uuid);
    	entity.setDeleted(Constants.DATA_NOT_DELETED);
    	entity.setCreateTime(date);
    	integrationMessageService.save(entity);
    	
    	IntegrationMessageContentEntity contentEntity = handleHexData(responseMessage);
    	contentEntity.setMessageId(uuid);
    	/**
    	 * 油站id
    	 */
    	contentEntity.setStationId(entity.getStationId());
    	contentEntity.setDeleted(Constants.DATA_NOT_DELETED);
    	contentEntity.setCreateTime(date);
    	integrationMessageContentService.save(contentEntity);
    	
    	return DataResult.success();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
    @PostMapping("/list")
    @ResponseBody
    public DataResult list(@RequestBody IntegrationMessageEntity integrationMessage){
        Page page = new Page(integrationMessage.getPage(), integrationMessage.getLimit());
        LambdaQueryWrapper<IntegrationMessageEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(IntegrationMessageEntity::getId, integrationMessage.getId());
        IPage<IntegrationMessageEntity> iPage = integrationMessageService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }
    
    private IntegrationMessageContentEntity handleHexData(String str){
    	IntegrationMessageContentEntity entity = new IntegrationMessageContentEntity();
    	// String str = "FA000100010581FFFFFFFF016200013200000F540020250225114410010004134000000001300298ADF30000000000000000000000000000000000000000000000000000000000000000000000000000000000011040000000030E00006637D90000000000000000000000000000004E27";
    	str = str.substring(0, str.length() - 4); // 去掉后面四位校验码
    	String str1 = str.substring(12);
    	String str2 = str1.substring(0,2);
    	if(str2.equals("81")){
    		// 处理81 poll
    		String hexMessageCount = str1.substring(10,12);
    		int MessageCount = Integer.parseInt(hexMessageCount, 16);
    		log.info("MessageCount:" + MessageCount);
    		String hexData = str.substring(24);
    		
    		for (int i = 1; i <= MessageCount; i++) {
    			String hexLength = hexData.substring(0,2);
        		int length = Integer.parseInt(hexLength,16);
        		int lengthSize  = length * 2;
        		String data = hexData.substring(0, lengthSize + 2);
        		
        		log.info("data:"+data);
        		String messageTpye = data.substring(6, 8);
        		log.info("messageTpye:"+messageTpye);
        		// 处理32 
        		if(messageTpye.equals("32")){
        			String _data = data.substring(8);
            		
        			log.info("交易流水号:"+_data.substring(0, 8));//交易流水号 8
        			entity.setTransactionNumber(_data.substring(0, 8));

        			log.info("交易类型:"+_data.substring(8, 10));// 交易类型 2
        			entity.setTransactionType(_data.substring(8, 10));

        			log.info("日期及时间:"+_data.substring(10, 24));// 日期及时间 14
        			entity.setTransactionDate(_data.substring(10, 24));

        			log.info("卡号:"+_data.substring(24, 44));// 卡号 20
        			entity.setTransactionCard(_data.substring(24, 44));

        			log.info("余额:"+_data.substring(44, 52));// 余额 8
        			entity.setBalance(Integer.parseInt(_data.substring(44, 52),16) + "");

        			log.info("数额:"+_data.substring(52, 58));// 数额 6
        			entity.setAmount(Integer.parseInt(_data.substring(52, 58),16) + "");

        			log.info("备用1:"+_data.substring(58, 134));// 备用1 76
        			entity.setStandby1(_data.substring(58, 134));

        			log.info("枪号:"+_data.substring(134, 136));// 枪号 2
        			entity.setGunNumber(_data.substring(134, 136));

        			log.info("油品代码:"+_data.substring(136, 140));// 油品代码 4
        			entity.setOils(_data.substring(136, 140));

        			log.info("升数:"+_data.substring(140, 146));// 升数 6
        			entity.setLitres(Integer.parseInt(_data.substring(140, 146),16) + "");

        			log.info("成交价格:"+_data.substring(146, 150));// 成交价格 4
        			entity.setPrice(Integer.parseInt(_data.substring(146, 150),16) + "");

        			log.info("员工号:"+_data.substring(150, 152));// 员工号 2
        			entity.setEmployeeNumber(_data.substring(150, 152));

        			log.info("升累计:"+_data.substring(152, 160));// 升累计 8
        			entity.setRiseTotal(Integer.parseInt(_data.substring(152, 160),16) + "");

        			log.info("备用2:"+_data.substring(160, 190));// 备用2 30
        			entity.setStandby2(_data.substring(160, 190));
        			
            		hexData = hexData.substring(lengthSize + 2);
            		log.info("下一个hexData:"+hexData);
        		}
			}
    	}
    	return entity;
    	
    }
    
    
    public static void main(String[] args) {
		
    	String str = "FA000100010581FFFFFFFF016200013200000F540020250225114410010004134000000001300298ADF30000000000000000000000000000000000000000000000000000000000000000000000000000000000011040000000030E00006637D90000000000000000000000000000004E27";
    	str = str.substring(0, str.length() - 4); // 去掉后面四位校验码

    	String str1 = str.substring(12);
    	String str2 = str1.substring(0,2);
    	if(str2.equals("81")){
    		// 处理81 poll
    		String hexMessageCount = str1.substring(10,12);
    		int MessageCount = Integer.parseInt(hexMessageCount, 16);
    		log.info("MessageCount:" + MessageCount);
    		String hexData = str.substring(24);
    		
    		for (int i = 1; i <= MessageCount; i++) {
    			String hexLength = hexData.substring(0,2);
        		int length = Integer.parseInt(hexLength,16);
        		int lengthSize  = length * 2;
        		String data = hexData.substring(0, lengthSize + 2);
        		
        		log.info("data:"+data);
        		String messageTpye = data.substring(6, 8);
        		log.info("messageTpye:"+messageTpye);
        		// 处理32 
        		if(messageTpye.equals("32")){
        			String _data = data.substring(8);
            		
        			log.info(_data.substring(0, 8));//交易流水号 8
        			log.info(_data.substring(8, 10));// 交易类型 2
        			log.info(_data.substring(10, 24));// 日期及时间 14
        			log.info(_data.substring(24, 44));// 卡号 20
            		log.info(_data.substring(44, 52));// 余额 8
            		log.info(_data.substring(52, 58));// 数额 6
            		log.info(_data.substring(58, 134));// 备用1 76
            		log.info(_data.substring(134, 136));// 枪号 2
            		log.info(_data.substring(136, 140));// 油品代码 4
            		log.info(_data.substring(140, 146));// 升数 6
            		log.info(_data.substring(146, 150));// 成交价格 4
            		log.info(_data.substring(150, 152));// 员工号 2
            		log.info(_data.substring(152, 160));// 升累计 8
            		log.info(_data.substring(160, 190));// 备用2 30
            		
            		hexData = hexData.substring(lengthSize + 2);
            		log.info("下一个hexData:"+hexData);
        		}
			}
    	}
	}
}
